<?php

require_once 'classes/LightPressPlugin.php';

class CategoryTemplate extends LightPressPlugin {
    
    var $default_context = LP_CONTEXT_CATEGORY;
    var $active = true;
    var $hooks = array('start_render');

    var $description = 'Uses a separate template for each category of the form "category-yourcategoryname.xml".  If the file is missing, defaults to the basic template.';
    
    var $_frontend;
    
    function CategoryTemplate(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$templates) {
        $f =& $this->_frontend;
		if (!$this->active)
			return;
        
        /* get category name & build template filename */
		$context =& $f->context;
        switch ($context['type']) {
			case 'category':    //use category "nicename"
                $categories =& $f->getCategories();
				if (isset($categories[$context['filter']]['category_nicename']))
                    $catname = $categories[$context['filter']]['category_nicename'];
				break;
			default:    //unsupported context, try filter value and go for broke 
				$catname = $context['filter'];      // should work for pages
                break;
		}
        if (!isset($catname))
            return;
        $catplate = strtolower($f->context['type'] . '-' . $catname . '.xml');
        
        /* switch main template if category template exists */
        $themedir =& $f->tpl->root;
        if (is_dir($themedir) && file_exists($themedir . '/' . $catplate) )
            $templates['index'] = $catplate;

        $this->active = false;
        return;
	}	

    function hide() {
        // do nothing
    }

}

?>